local skel = fk.CreateSkill {
  name = "rmt__yuba",
}

local on_cost = function (self, event, target, player, data)
  if player.room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#rmt__yuba-invoke:::"..player:getMark("@rmt__yuba")}) then
    return true
  else
    player.room:addPlayerMark(player, "@rmt__yuba", 1)
  end
end

---@param player ServerPlayer
local on_use = function(self, event, target, player, data)
  local room = player.room
  local x = player:getMark("@rmt__yuba")
  local y = x - player:getHandcardNum()
  if y > 0 then
    player:drawCards(y, skel.name)
  end
  if player.dead then return end
  if #room:askToDiscard(player, {
    max_num = 1, min_num = 1, include_equip = true, skill_name = skel.name, pattern = ".|"..x, cancelable = true,
    prompt = "#rmt__yuba-discard:::"..x,
  }) == 0 then
    local skills = table.map(table.filter(player.player_skills, function (s)
      return s:isPlayerSkill(player) and s.visible
    end), Util.NameMapper)
    if #skills > 0 then
      local skill = room:askToChoice(player, {choices = skills, prompt = "#rmt__yuba-lose", skill_name = skel.name, detailed = true })
      room:handleAddLoseSkills(player, "-"..skill)
    end
  end
end

skel:addEffect(fk.Damage, {
  anim_type = "drawcard",
  on_cost = on_cost,
  on_use = on_use,
})

skel:addEffect(fk.Damaged, {
  anim_type = "drawcard",
  on_cost = on_cost,
  on_use = on_use,
})


skel:addAcquireEffect(function (self, player, is_start)
  if player.tag[skel.name] ~= nil then
    player.room:setPlayerMark(player, "@rmt__yuba", player.tag[skel.name])
  end
end)

skel:addLoseEffect(function (self, player, is_death)
  if player:getMark("@rmt__yuba") ~= 0 then
    player.room:setPlayerMark(player, "@rmt__yuba", 0)
    player.tag[skel.name] = player:getMark("@rmt__yuba")
  end
end)

Fk:loadTranslationTable{
  ["rmt__yuba"] = "欲罢",
  [":rmt__yuba"] = "当你造成或受到伤害后，你可以将手牌摸至X张，弃置一张点数为X的牌或失去一个技能。（X为拒绝发动本技能次数）",

  ["#rmt__yuba-invoke"] = "欲罢：你可以将手牌摸至 %arg 张，然后须弃置一张 %arg 点牌，否则失去1个技能",
  ["@rmt__yuba"] = "欲罢",
  ["#rmt__yuba-discard"] = "欲罢：你需弃置一张 %arg 点牌，否则失去1个技能",
  ["#rmt__yuba-lose"] = "欲罢：请选择一个技能并失去",

  ["$rmt__yuba1"] = "昔年种柳，今看摇落。树犹如此，人何以堪。",
  ["$rmt__yuba2"] = "既不能流芳后世，不足复遗臭万载！",
}


return skel
